package com.wanmait.recycle.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.wanmait.recycle.pojo.Userinfo;
import com.wanmait.recycle.service.UserinfoService;
import com.wanmait.recycle.util.CookieUtil;

/**
 * Servlet Filter implementation class AutoLoginFilter
 */
@WebFilter(filterName="/AutoLoginFilter",urlPatterns="/*")
public class AutoLoginFilter implements Filter {
	

	
    /**
     * Default constructor. 
     */
    public AutoLoginFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession();

/*		String checkedLogin = (String)session.getAttribute("checkedLogin");*/
		//如果之前经过该过滤器进行过自动登录，不管成功或者失败都放行
		/*if(checkedLogin!=null)
		{
			chain.doFilter(request, response);
			return;
		}
		//如果是第一次经过该过滤器进行自动登录，则设置个标志
		else
		{
			session.setAttribute("checkedLogin", "true");
		}*/
			Userinfo userinfo = (Userinfo) session.getAttribute("userinfo");
	
		if (userinfo != null) {
			chain.doFilter(request, response);
			return;
		}
		// 自动登录
		String username = CookieUtil.getValue("username", req);
		String password = CookieUtil.getValue("password", req);
	
		// 之前没有记住我
		if (username == null) {
			chain.doFilter(request, response);
			return;
		}
	
		// 登录
		Userinfo user =new Userinfo();
		user.setName(username);
		user.setPassword(password);
		//注入service
        
        //获得ServletContext对象
		ServletContext sc = session.getServletContext();
		//获取WebApplicationContext
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(sc);
		//获得UserinfoService和DoctorService
		
		UserinfoService userinfoService;
		userinfoService = (UserinfoService)wac.getBean(UserinfoService.class);
		
		Userinfo Ruserinfo = userinfoService.findUserinfoByExample2(user);
		
		
		if (Ruserinfo != null) {
			// 登录成功
			
			session.setAttribute("userinfo", Ruserinfo);
			// 重新写cookie
			// 写Cookie
			CookieUtil.addCookie("username", username, "/", 60 * 60 * 24 * 30, res);
			CookieUtil.addCookie("password", Ruserinfo.getPassword(), "/", 60 * 60 * 24 * 30, res);
		}

		chain.doFilter(request, response);

	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
